<?php

namespace App\Jobs;

use Carbon\Carbon;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UserEnteredJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	/**
	 * @var User
	 */
    protected $user = null;
    
    public function __construct(User $user = null)
    {
        $this->user = $user;
    }
    
    public function handle()
    {
	    if ($this->user) {
		    $lastEnteredAt = $this->user->last_entered_at;
		    if (!$lastEnteredAt or ($lastEnteredAt->toDateString() !== Carbon::now()->toDateString())) {
			    $this->user->update(['last_entered_at' => Carbon::now()]);
		    }
	    }
    }
}
